package com.chengxiaoxiao.libarypartner.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.constants.Consrants;
import com.chengxiaoxiao.libarypartner.utils.ApiAddress;
import com.chengxiaoxiao.libarypartner.utils.LoadDialogUtils;
import com.chengxiaoxiao.libarypartner.utils.LoginUtils;
import com.chengxiaoxiao.libarypartner.utils.SPUtils;
import com.chengxiaoxiao.libarypartner.utils.ToastUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends Activity
{

    private EditText editPwd;
    private Button btnLogin;
    private EditText editUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editPwd = (EditText) findViewById(R.id.editPwd);
        editUserName = (EditText) findViewById(R.id.editUserName);
    }

    public void login(View v)
    {
        String pwd = editPwd.getText().toString().trim();
        String uid = editUserName.getText().toString().trim();

        if (TextUtils.isEmpty(uid))
        {
            ToastUtils.makeText(getApplicationContext(), "请输入用户名");
            return;
        }

        if (TextUtils.isEmpty(pwd))
        {
            ToastUtils.makeText(getApplicationContext(), "请输入密码");
            return;
        }

        login(uid,pwd);

    }

    private void login(final String uid, String pwd)
    {

        LoadDialogUtils.show(LoginActivity.this,"","登录中...");

        AsyncHttpClient client = new AsyncHttpClient();

        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("pwd",pwd);

        client.post(ApiAddress.getApiDomain() + "/client/ClientServlet?op=login",params, new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {

                LoadDialogUtils.hidden();
                String body = new String(responseBody);

                //ToastUtils.makeText(LoginActivity.this,body);
                try
                {
                    JSONObject jsonObj = new JSONObject(body);
                    String status = jsonObj.getString(Consrants.JSON_RESPONSE_KEY_STATUS);
                    String msg = jsonObj.getString(Consrants.JSON_RESPONSE_KEY_ERRORMSG);

                    if("error".equalsIgnoreCase(status))
                    {
                        ToastUtils.makeText(LoginActivity.this,msg);
                        return;
                    }
                    if("ok".equalsIgnoreCase(status))
                    {
                        //SPUtils.putString(AppConsrants.USER_LOGIN_MODEL_ID,msg,LoginActivity.this);

                        //设置登录标志,并保存用户名
                        LoginUtils.setLoginStatusTrue(LoginActivity.this);

                        SPUtils.putString(Consrants.SP_LOGIN_USERNAME,uid,LoginActivity.this);


                        startActivity(new Intent(LoginActivity.this, MainActivity.class));

                        finish();
                    }

                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                LoadDialogUtils.hidden();
                ToastUtils.makeText(getApplication(), "网络连接错误");
            }
        });

    }
}
